from django.db import models

from users.models import User
from web_testing.config import PRIORITY_CHOICES, COMMAND_CHOICES, LOCATE_CHOICES, STATUS_CHOICES


# Create your models here.
class WebProject(models.Model):
    """WEB测试项目类模型定义"""
    user = models.ForeignKey(User, on_delete=models.SET_NULL, verbose_name='所属用户', null=True, blank=True)
    project_code = models.CharField(max_length=100, verbose_name='项目编号')
    project_name = models.CharField(max_length=100, verbose_name='项目名称')
    project_description = models.TextField(max_length=400, verbose_name='项目描述', null=True, blank=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_web_project'
        verbose_name = 'web测试项目'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.project_name


class WebCase(models.Model):
    """WEB测试用例类模型定义"""
    web_project = models.ForeignKey(WebProject, on_delete=models.SET_NULL, verbose_name='所属项目', null=True, blank=True)
    case_code = models.CharField(max_length=100, unique=True, verbose_name='用例编号')
    case_name = models.CharField(max_length=100, verbose_name='用例标题')
    priority = models.IntegerField(choices=PRIORITY_CHOICES, default=0, verbose_name='优先级')
    pre_case = models.ForeignKey('self', on_delete=models.SET_NULL, verbose_name='前置用例', null=True, blank=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    status = models.IntegerField(choices=STATUS_CHOICES, default=0, verbose_name='执行状态')
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_web_case'
        verbose_name = 'WEB测试用例'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.case_name


class WebCaseStep(models.Model):
    """WEB测试用例步骤类模型定义"""
    web_case = models.ForeignKey(WebCase, on_delete=models.SET_NULL, verbose_name='所属用例', null=True, blank=True)
    sequence = models.SmallIntegerField(verbose_name='步骤顺序')
    step_description = models.CharField(max_length=100, verbose_name='步骤描述')
    command = models.CharField(choices=COMMAND_CHOICES, max_length=100, verbose_name='步骤命令', null=True, blank=True)
    locate_method = models.CharField(choices=LOCATE_CHOICES, max_length=20, verbose_name='定位方式', null=True, blank=True)
    locate_value = models.CharField(max_length=400, verbose_name='页面元素、具体值', null=True, blank=True)
    value = models.CharField(max_length=400, verbose_name='数据项', null=True, blank=True)

    class Meta:
        db_table = 'tb_web_case_step'
        verbose_name = 'WEB测试用例步骤'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.step_description
